	//
	//  GeneralWebViewController.m
	//  FrederictonTourGuide
	//
	//  Created by KashFey on 11-03-15.
	//  Copyright 2011 __MyCompanyName__. All rights reserved.
	//

#import "GeneralWebViewController.h"


@implementation GeneralWebViewController

@synthesize myWebView, jsString;

- (IBAction)Done:(id)sender
{
		// open a alert with an OK and cancel button
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:@"<This action will dismiss the WebView!>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
	[alert show];
	[alert release];
}

	// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
	- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
	// Custom initialization.
	}
	return self;
	}
	*/


	// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	[super viewDidLoad];
	
	NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/us/app/iquiksplash-free-2in1/id359017259?mt=8"] encoding:NSASCIIStringEncoding error:nil];
	
	if (connected == NULL)
	{
		NSLog(@"WebView Not connected");
			//The location operation failed, display the error
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed Loading Map Services!"
																																																						message:@"Make sure device is connected to a network and then try relaunching the application."
																																																					delegate:nil
																																												cancelButtonTitle:@"OK"
																																												otherButtonTitles:nil];
		[alertView show];
		[alertView release];
		
	} 
	else 
	{
		NSLog(@"WebView Connected");
			//NSLog(@"JsString:%@", jsString);
		NSString * param  = jsString;   
		NSString * jsCallBack = [NSString stringWithFormat:@"ocSearchText=('%@')",param];
		[myWebView stringByEvaluatingJavaScriptFromString:jsCallBack];
		
			self.myWebView.scalesPageToFit = YES;
			self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
		[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Route" ofType:@"html"]isDirectory:NO]]];
	}
}

 // Override to allow orientations other than the default portrait orientation.
/*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
		// Return YES for supported orientations.
 return YES;
}*/

#pragma mark -
#pragma mark UIViewController delegate methods

- (void)viewWillAppear:(BOOL)animated {
	self.myWebView.delegate = self; // setup the delegate as the web view is shown
																																	//[[[self.myWebView subviews] lastObject] setScrollEnabled:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
	[self.myWebView stopLoading];   // in case the web view is still loading its content
	self.myWebView.delegate = nil;  // disconnect the delegate as the webview is hidden
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // turn off the twirly
}

-(void)viewDidAppear:(BOOL)animated 
{
}

#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView {
		// NSLog(@"%s", __FUNCTION__);
		// starting the load, show the activity indicator (twirly) in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
		// NSLog(@"%s %@", __FUNCTION__, [[[webView request] URL] absoluteString]);
		// finished loading, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
		// load error, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
		// report the error inside the webview
		// NSLog(@"webView didFailLoadWithError: %@, %d", [webView.request URL], [error code]);
	if (error.code != -999) {
		NSString* errorString =
		[NSString stringWithFormat:
			@"<html><style>\n"
			@"body {background-color: #ccc; margin-top: 50px;}\n"
			@".e1 { font-family: verdana, sans-serif; color:#f66; font-size: 32px; font-weight:bold; text-align: center; }\n"
			@".e2 { font-family: verdana; color:#066; font-size: 32px; font-weight:bold; text-align: center; }\n"
			@".u1 { font-family: monospace; color:#000; font-size: 24px; text-align: center; }\n"
			@"</style>\n"
			@"<p class='e1'>Error fetching web page:</p>\n"
			@"<p class='u1'>%@</p>\n"
			@"<p class='e2'>%@</p>\n"
			@"</html>",
			[[[webView request] URL] absoluteString],
			error.localizedDescription];
		[self.myWebView loadHTMLString:errorString baseURL:nil];
	}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
  // the user clicked one of the OK/Cancel buttons
	if (!buttonIndex == 0)
	{
		[self dismissModalViewControllerAnimated:YES];
	}
}

- (void)didReceiveMemoryWarning {
		// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];
	
		// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
	[super viewDidUnload];
		// Release any retained subviews of the main view.
		// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[super dealloc];
}

@end
